package com.fancy.learncenter.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonRealeaseSpeakDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.adapter.CartoonExpandListenReleaseAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * A fragment with a Google +1 button.
 */
public class CartoonExpandReleaseListenFragment extends Fragment {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    public CartoonExpandReleaseListenFragment() {
        // Required empty public constructor
    }

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_cartoon_release_listen, container, false);
        ButterKnife.bind(this, rootView);

        initView();
        getDatas();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    CartoonExpandListenReleaseAdapter adapter;

    private void initView() {

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        List<CartoonRealeaseSpeakDataBean.ResultBean> list = new ArrayList<>();
        adapter = new CartoonExpandListenReleaseAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);
    }


    private void getDatas() {
        Map map = new HashMap();
        map.put("classId", MyApplication.classIDStr);
        map.put("type", "2");
        HttpMehtod.getInstance().releaseSpeaking(map, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonRealeaseSpeakDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonRealeaseSpeakDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonRealeaseSpeakDataBean>() {

                    @Override
                    public void success(CartoonRealeaseSpeakDataBean dataBean) {
//                        mFragmentAdapteradapter.notifyDataSetChanged();
                        adapter.addAll(dataBean.getResult());
                    }

                    @Override
                    public void fail(String errorCode) {
                    }
                }).dealData();

            }
        });
    }
}
